import MiniGamesSettings from "./MiniGamesSettings";
import settings from "./settings";

export let guiMover = {
    gui : new Gui(),
    DefaultSettings: {},
    settings: {
        WitherCloak : { x:120,y:100,s:1.0,w:62,h:45, show(){ return settings.WitherCloakTimer; } },
        KillCombo : { x:50,y:100,s:1.0,w:75,h:38, show(){ return settings.KillCombo; } },
        FlareTimer : { x:50,y:80,s:1.5,w:70,h:9, show(){ return settings.Flaretimer; } },
        PigmanSword : { x:50,y:60,s:1.5,w:50,h:9, show(){ return settings.pigmansword; } },
        WitherShield : { x:40,y:50,s:1.5,w:50,h:9, show(){ return settings.WitherShield > 0; } },
        RagnarockAxe : { x:50,y:50,s:1.5,w:60,h:9, show(){ return settings.RagnarockAxe > 0; } },
        ReaperArmor : { x:60,y:50,s:1.5,w:50,h:9, show(){ return settings.ReaperArmor; } },
        EndStoneSword : { x:60,y:60,s:1.0,w:45,h:18, show(){ return settings.EndStoneSword > 0; } },
        SlayerBoss : { x:190,y:100,s:1.5,w:100,h:9, show(){ return settings.SlayerBossInfo; } },
        KuudraHP : { x:200,y:20,s:2.0,w:35,h:9, show(){ return settings.ShowKuudraHP === 1; } },
        KuudraStun : { x:100,y:260,s:2.5,w:120,h:9, show(){ return settings.stun; } },
        Dropship : { x:100,y:200,s:2.5,w:50,h:9, show(){ return settings.Dropship; } },
        EnergyCharge : { x:100,y:0,s:1.5,w:70,h:9, show(){ return settings.Fuel; } },
        InKuudraStun : { x:300,y:10,s:1.5,w:70,h:27, show(){ return settings.stuntoggle; } },
        attPrice : { x: Renderer.screen.getWidth() / 6 , y: Renderer.screen.getHeight() / 6 , s:1.0,w:200,h:36, show(){ return settings.AttributesPrice; } },
        Scatha : { x:100,y:160,s:1.5,w:62,h:9, show(){ return settings.Scatha > 0; } },
        Dillo : { x:100,y:180,s:1.5,w:70,h:9, show(){ return settings.ArmadilloEnergy > 0; } },
        ReheatedGummyPolarBear : { x:300,y:200,s:1.0,w:34,h:16, show(){ return settings.ReheatedGummyPolarBear > 0; } },
        SuspiciousScrap : { x:400,y:110,s:1.0,w:34,h:16, show(){ return settings.GlaciteMineshaftWaypoints; } },
        SkywarsAOTECount : { x:400,y:300,s:1.0,w:26,h:9, show(){ return MiniGamesSettings.AspectOfTheEndCount; } },
        DevilsContract : { x:400,y:350,s:1.0,w:68,h:9, show(){ return MiniGamesSettings.FastDevilsContract; } },
        Compass : { x:400,y:370,s:1.0,w:16,h:16, show(){ return MiniGamesSettings.MaybeIhaveCompass > 0; } },
        showSackItems : { x:200,y:100,s:1.0,w:80,h:27, show(){ return settings.showSackItems > 0; } },
        quiver : { x:200,y:200,s:1.0,w:80,h:16, show(){ return settings.ShowArrows; } },
    },
    Selected : "",
    add (name,x,y,s,w,h) {
        this.DefaultSettings[name] = {x:x,y:y,s:s,w:w,h:h};
        if(!this.settings[name]) this.settings[name] = {x:x,y:y,s:s,w:w,h:h};
        this.SaveFiles();
    },
    SaveFiles () {
        let SaveObj = {};
        for (let setting in this.settings) {
            SaveObj[setting] = {
                x: this.settings[setting].x,
                y: this.settings[setting].y,
                s: this.settings[setting].s
            };
        }
        FileLib.write("./config/ChatTriggers/modules/BabyzombieAddons/config/HudLocation.json",JSON.stringify(SaveObj,null,4),true);
    },
    reset () {
        for (let key in this.settings) {
            this.settings[key].x = this.DefaultSettings[key].x;
            this.settings[key].y = this.DefaultSettings[key].y;
            this.settings[key].s = this.DefaultSettings[key].s;
        }
    }
};

for (let key in guiMover.settings) {
    guiMover.DefaultSettings[key] = {x:guiMover.settings[key].x,y:guiMover.settings[key].y,s:guiMover.settings[key].s};
}

if(FileLib.isDirectory("./config/ChatTriggers/modules/BabyzombieAddons/config") && FileLib.exists("./config/ChatTriggers/modules/BabyzombieAddons/config/HudLocation.json")) {
    try {
        const SettingFile = JSON.parse(FileLib.read("./config/ChatTriggers/modules/BabyzombieAddons/config/HudLocation.json"));
        for (let setting in guiMover.settings) {
            if(SettingFile[setting] != undefined) {
                guiMover.settings[setting].x = SettingFile[setting].x;
                guiMover.settings[setting].y = SettingFile[setting].y;
                guiMover.settings[setting].s = SettingFile[setting].s;
            }
        }
    } catch (error) {}
}
guiMover.SaveFiles();

guiMover.gui.registerDraw(Draw);
function Draw(mx,my,ticks) {
    Renderer.drawRect(
        Renderer.color(0, 0, 0, 70),
        0,
        0,
        Renderer.screen.getWidth(),
        Renderer.screen.getHeight()
    );
    for (let setting in guiMover.settings) {
        if(guiMover.settings[setting].show()) 
        Renderer.drawRect((guiMover.Selected == setting ? Renderer.color(255,255,255,60) : Renderer.color(0,0,0,45)) , guiMover.settings[setting].x , guiMover.settings[setting].y , guiMover.settings[setting].w * guiMover.settings[setting].s , guiMover.settings[setting].h * guiMover.settings[setting].s );
    }
}

guiMover.gui.registerClicked(Clicked);
function Clicked(mx,my,button) {
    for (let setting in guiMover.settings) {
        if( guiMover.settings[setting].show() &&
            mx > guiMover.settings[setting].x && mx < guiMover.settings[setting].x + guiMover.settings[setting].w * guiMover.settings[setting].s && 
            my > guiMover.settings[setting].y && my < guiMover.settings[setting].y + guiMover.settings[setting].h * guiMover.settings[setting].s ) {
                guiMover.Selected = setting;
        }
    }
}

register("dragged",Dragged)
function Dragged(dx,dy,mx,my,button) {
    if(!guiMover.Selected) return;
    if(guiMover.settings[guiMover.Selected].x + dx >= 0 && guiMover.settings[guiMover.Selected].x + dx <= Renderer.screen.getWidth() - guiMover.settings[guiMover.Selected].w * guiMover.settings[guiMover.Selected].s) guiMover.settings[guiMover.Selected].x = guiMover.settings[guiMover.Selected].x + dx;
    if(guiMover.settings[guiMover.Selected].y + dy >= 0 && guiMover.settings[guiMover.Selected].y + dy <= Renderer.screen.getHeight() - guiMover.settings[guiMover.Selected].h * guiMover.settings[guiMover.Selected].s) guiMover.settings[guiMover.Selected].y = guiMover.settings[guiMover.Selected].y + dy;
}

guiMover.gui.registerScrolled(Scrolled)
function Scrolled(mx,my,scroll) {
    if(!guiMover.Selected) return;
    if(guiMover.settings[guiMover.Selected].s + scroll / 10 > 0) guiMover.settings[guiMover.Selected].s += scroll / 10;
}

guiMover.gui.registerMouseReleased(Released);
guiMover.gui.registerClosed(Released);
function Released() {
    guiMover.Selected = "";
    guiMover.SaveFiles();
}

const keyBindInventory = new KeyBind(Client.getMinecraft().field_71474_y.field_151445_Q);
guiMover.gui.registerKeyTyped((typed,key) => {
    if(key === keyBindInventory.getKeyCode()) guiMover.gui.close();
})